<?php
/**
 * 道具箱
 * 
 * Created	: 2011-08-19
 * Modified : 2011-08-19
 * @author  : 陈立强
 */
class PropsBox
{
	
	public static $itemList = array();
	
	/**
	 * 获取列表
	 * @param int $uid
	 * @param string $fields
	 * @param string $where
	 */
	public static function getList($uid, $fields='*', $where='')
	{
		global $DB;
		if (empty($fields)) {
			$fields = '*';
		}
		if(!$where){
			$where = 'uid='.$uid;
		}
		$sql = 'SELECT '.$fields.' FROM props_box WHERE '.$where;
		return $DB->getList($sql);
	}
	
	/**
	 * 读取用户道具箱物品列表
	 * @param int $u_id 用户ID
	 * @param int $pageNo 道具箱分页编号 0-表示没有分页,显示全部
	 */
	public static function show($uid, $pageNo=1)
	{
		global $DB, $L, $G;
		
		$pageNo = (int)$pageNo;
		
		$pageSize = $G['propsbox_grid_page'];
		$where = 'uid='.$uid.' AND num<>0';
		
		$data = array();
		
		$sql = 'SELECT * FROM props_box WHERE '.$where;
		if ($pageNo > 0) {
			$total = $DB->getCount('props_box', $where);
			$pageNum = ceil($total/$pageSize);
			$start = ($pageNo-1)*$pageSize;
			$sql .= ' limit '.$start.', '.$pageSize;
			$data['cur_page'] = $pageNo;
			$data['max_page'] = $pageNum;
		}
		$arr = $DB->getList($sql);
		if (false === $arr) {
			return array(
				'val' => false,
				'msg' => $L['Failure']
			);
		}
		$itemList = C('item/item_list');
		$list = array();
		foreach ($arr as $val) {
			$cfg = $itemList[$val['item_id']];
			$list[] = array(
				'title' => $cfg['title'],
			);
		}
		
		$data['list'] = $list;
		
		return array(
			'val'  => true,
			'data' => $data,
		);
	}
	
	/**
	 * 物品存入道具箱
	 * @param int $uid 用户ID
	 * @param array $data 物品进入道具箱的一些参数item_id,num,quality
	 */
	public static function in($uid, $data)
	{
		global $DB, $L, $G;
		
		$itemId		= (int)$data['item_id'];

		if (empty(self::$itemList)) {
			$titems   = C('item/item_list');
			$itemInfo = $titems[$itemId];
			self::$itemList = $titems;
		}
		if (isset($data['title'])) {
			$title = $data['title'];
		} else {
			$title = $itemInfo['title'];
		}
		
		$num = (int)$data['num'];
		$lvl = isset($data['lvl']) ? (int)$data['lvl'] : 0;
		
		$where = 'uid=:uid and item_id=:item_id';
		$params = array(
			'uid'     => $uid,
			'item_id' => $itemId,
		);
		
		$sql = 'SELECT box_id,num FROM props_box WHERE '.$where;
		$boxInfo = $DB->getValue($sql, $params);
		if (!$boxInfo) {
			$data = array(
				'uid'		=> $uid,
				'item_id'	=> $itemId,
				'num'		=> $num,
				'title'		=> $title,
				'lvl'		=> $lvl,
			);
			$ret = $box_id = $DB->insert('props_box', $data);
		} else {
			$formerNum = (int)$boxInfo['num'];
			$num += $formerNum;
			$data = array(
				'num' => $num
			);
			$where = 'box_id='.$boxInfo['box_id'];
			$ret = $DB->update('props_box', $data, $where);
			$box_id = $boxInfo['box_id'];
		}
		
		$result['val'] = $ret;
		if ($ret) {
			$result['data']['box_id'] = $box_id;
		} else {
			$s = $itemId.': '.$title;
			$result['msg'] = sprintf($L['box_in_failure'], $s);
			$data = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('box_in', $data);
		}
		return $result;
	}

	/**
	 * 获取指定道具箱物品的信息
	 * @param int $uid
	 * @param int $box_id
	 */
	public static function getInfo($uid, $box_id)
	{
		global $DB, $L, $G;
		
		$box_id = (int)$box_id;
		
		$sql = 'SELECT * FROM props_box WHERE box_id='.(int)$box_id;
		$info = $DB->getValue($sql);
		if (!$info) {
			return array(
				'val' => false,
				'msg' => $L['Unknow_item']
			);
		}
		
		if ($uid != $info['uid']) {
			return array(
				'val' => false,
				'msg' => $L['Invalid_uid']
			);
		}

		return $info;
	}
}










